package com.yunche.iov.controller;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunche.iov.common.Result;
import com.yunche.iov.entity.Device;
import com.yunche.iov.service.DeviceManageService;


@RestController("deviceManageController")
@Scope("prototype")
public class DeviceManageController {

	
	@Autowired
	private DeviceManageService deviceManageService;
	
	/**
	 * 新增设备数据
	 * @param device 接收新增设备数据
	 * */
	@RequestMapping(value="/insertDevice",method=RequestMethod.POST)
	public String insertDevice(Device device)
	{
		
		return "";
	}
	/**
	 * 删除设备数据
	 * @param deviceIds 接收新增设备数据
	 * */
	@RequestMapping(value="/deleteDeviceById",method=RequestMethod.POST)
	public Result<Device> deleteDeviceById(String deviceIds)
	{
	
		Result<Device> ru = new Result<Device>();
		String[] strIds = deviceIds.split(",");
		
		try{
			for(String deviceId : strIds)
			{
			deviceManageService.deleteDeviceById(Integer.valueOf(deviceId));
			}
			
			return ru;
		}catch(Exception e)
		{
			ru.setCode(1);
			ru.setMsg("Failure");
			return ru;
		}
	
	}
	/**
	 * 更新设备数据
	 * @param device 更新设备数据的ID
	 * */
	@RequestMapping(value="/updateDevice",method=RequestMethod.POST)
	public void updateDevice(Device device)
	{
		deviceManageService.updateDevice(device);
	}
	/**
	 * 查询所有的设备
     * @param 
	 * @return 
	 * */
	@RequestMapping(value="/findAllDevice",method=RequestMethod.POST)
	public  Result<List<Device>> findAllDevice(PageInfo<Device> page)
	{
		//告诉mybatis拦截器只查询15条
		PageHelper.startPage(page.getPageNum(), 15);
		List<Device> devices = deviceManageService.findAllDevice();
		PageInfo<Device> pageInfo = new PageInfo<Device>(devices);
		
		Result<List<Device>> ru = new Result<List<Device>>();
		ru.setData(pageInfo.getList());
		ru.setCount(pageInfo.getTotal());
		return ru; 
      
		//return 
	}
	/**
	 * 根据ID查询设备
	 * @param device 设备的ID
	 * */
	@RequestMapping(value="/findDeviceById",method=RequestMethod.POST)
	public List<Device> findDeviceByCriteria(Device device)
	{
		List<Device> devices = deviceManageService.findDeviceByCriteria(device);
		return devices;
	}
	
}
